package com.ruoyi.common.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.util.Base64;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @ClassName HashUtil
 * @Description TODO
 * @Author zz
 * @Date 2023/12/4
 **/
public class HashUtil {
    /**
     * @Desc: 随机生成一个长度1-20的由数字和小写字母组成的字符串
     * @author: zz
     * @date: 2023/12/4
     */
    public static String generateUserSeed() {
        int length = (int) (Math.random() * 20) + 1;
        StringBuilder userSeed = new StringBuilder();
        String characters = "abcdefghijklmnopqrstuvwxyz0123456789";
        for (int i = 0; i < length; i++) {
            int randIndex = (int) (Math.random() * characters.length());
            userSeed.append(characters.charAt(randIndex));
        }
        return userSeed.toString();
    }
    /**
     * @Desc: 随机生成的一个长度26的由数字和大小写字母组成的字符串
     * @author: zz
     * @date: 2023/12/4
     */
    public static String generateServerSeed() {
        // Generating a random string of length 26
        int length = 26;
        StringBuilder serverSeed = new StringBuilder();
        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        for (int i = 0; i < length; i++) {
            int randIndex = (int) (Math.random() * characters.length());
            serverSeed.append(characters.charAt(randIndex));
        }
        return serverSeed.toString();
    }

    public static int generateNonce() {
        Random random = new Random();
        // 生成1到100之间的随机整数
        return random.nextInt(100) + 1;
    }

    /**
     * @Desc: 计算公共hash值
     * @author: zz
     * @date: 2023/12/4
     */
    public static String commonHashCode(String userSeed, String serverSeed,int nonce) {
        String combinedSeed = userSeed + serverSeed + nonce;
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(combinedSeed.getBytes());
            StringBuilder hexString = new StringBuilder();

            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }

            return hexString.toString().substring(0,15);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

//    public static String commonHashCode(String userSeed, String serverSeed) {
//        try {
////            byte[] keyBytes = userSeed.getBytes();
////            SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "HmacSHA256");
////            Mac mac = Mac.getInstance("HmacSHA256");
////            mac.init(keySpec);
////            byte[] serverSeedBytes = serverSeed.getBytes();
////            byte[] hmacBytes = mac.doFinal(serverSeedBytes);
////            String hexString =  Base64.getEncoder().encodeToString(hmacBytes);
////            return  hexString.substring(0, 15);
//
//            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
//            SecretKeySpec secret_key = new SecretKeySpec(userSeed.getBytes(), "HmacSHA256");
//            sha256_HMAC.init(secret_key);
//            byte[] signature = sha256_HMAC.doFinal(serverSeed.getBytes(CharsetUtil.CHARSET_UTF_8));
//            return HexUtil.encodeHexStr(signature).substring(0,15);
//        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    /**
     * @Desc: 计算roll值
     * @author: zz
     * @date: 2023/12/4
     */
    public static Long rollValue(String hexString){
        // Calculate roll value
        long rollValue = Long.parseLong(hexString, 16) % 1000000 + 1;
        return rollValue;
    }

    /**
     * @Desc: 计算roll值
     * @author: zz
     * @date: 2023/12/4
     */
    public static Long rollValue1(String hexString){
        // Calculate roll value
        long rollValue = Long.parseLong(hexString, 16) % 10000 + 1;
        return rollValue;
    }

    // 生成4-6位随机推广码
    public static String generatePromoCode() {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        int length = new Random().nextInt(3) + 4; // 随机生成长度为4到6的随机数

        return new Random().ints(length, 0, characters.length())
                .mapToObj(characters::charAt)
                .map(Object::toString)
                .collect(Collectors.joining());
    }
}
